<template>
  <div class="cart">
    <div class="title">已选商品</div>
    <table class="table">
      <thead class="thead">
        <tr>
          <th>id</th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) of cartlist" :key="index">
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.price * item.num}}</td>
          <td>{{ item.num }}</td>
          <td>
            <div class="btn" @click="del(item)">删除</div>
          </td>
        </tr>
        <tr v-if="!cartlist.length">
            购物车空空如也...
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script>
import { mapActions, mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters('cart', ['cartlist'])
  },
  methods: {
    ...mapActions('cart', ['delproduct']),
    del (val) {
      this.delproduct(val)
    }
  }
}
</script>
<style lang="scss" scoped>
.cart {
  .title {
    height: 30px;
    text-align: center;
    line-height: 30px;
  }
  .table {
    width: 100%;
    text-align: center;
    .btn {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 50px;
      height: 20px;
      background: red;
      font-size: 12px;
      text-align: center;
      line-height: 20px;
      border-radius: 2px;
    }
    tr {
      height: 60px;
      line-height: 60px;
      td {
        position: relative;
      }
    }
    .null {
      width: 100%;
      height: 30px;
      line-height: 30px;
      text-align: center;
    }
  }
  .table * {
    border: 1px solid #000;
  }
}
</style>
